package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.common.constant.BusinessStatusConstants;
import com.qk.common.constant.ClueStatusConstants;
import com.qk.domain.business.BusinessDo;
import com.qk.domain.clue.ClueDO;
import com.qk.dto.business.BusinessDTO;
import com.qk.dto.business.BusinessListDTO;
import com.qk.dto.business.BusinessPoolDTO;
import com.qk.dto.business.UpdateBusinessInfoDTO;
import com.qk.dto.clue.UpdateClueInfoDTO;
import com.qk.entity.Business;
import com.qk.management.aop.anno.LogAnno;
import com.qk.management.service.BusinessService;
import com.qk.vo.business.BusinessVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 *
 * @author MrZhao
 * @created 2025-10-15 10:54
 * @description
 *  商机Controller
 *
 */
@Slf4j
@RestController
@RequestMapping("/businesses")
public class BusinessController {
    @Autowired
    private BusinessService businessService;
    //跟进商机
    @LogAnno
    @PutMapping
    public Result updateBusinessInfoById(@RequestBody UpdateBusinessInfoDTO dto) {
        // TODO 验证每一个请求都会开辟一条线程池中线程处理
        log.info("=========================Controller 中 方法updateBusinessInfoById 处理线程名称是：{}",Thread.currentThread().getName());
        businessService.updateBusinessInfoById(dto);
        return Result.success();
    }


    @GetMapping("/pool")
    public Result getPool(BusinessPoolDTO dto){
        PageResult<Business> result = businessService.getPool(dto);
        return Result.success(result);
    }

    @LogAnno
    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id) {
        businessService.toCustomer(id);
        return Result.success();
    }
    @PutMapping("/back/{id}")
    public Result back(@PathVariable Integer id) {
        businessService.updateById(Business.builder()
                .id(id)
                .status(BusinessStatusConstants.RECYCLE)
                .updateTime(LocalDateTime.now())
                .build());
        return Result.success();
    }
    @PutMapping("/assign/{businessId}/{userId}")
    public Result assign(@PathVariable Integer businessId, @PathVariable Integer userId) {
        businessService.updateById(Business.builder()
                .id(businessId)
                .userId(userId)
                .updateTime(LocalDateTime.now())
                .status(BusinessStatusConstants.WAIT_FOLLOW_UP)
                .build());
        return Result.success();
    }

    @GetMapping
    public Result list(BusinessListDTO dto){
        PageResult<BusinessDo> result = businessService.businessList(dto);
        return Result.success(result);
    }
    @LogAnno
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id){
        BusinessVO vo =businessService.getBusinessInfoById(id);
        return Result.success(vo);
    }
    @LogAnno
    @PostMapping
    public Result addBusiness(@RequestBody BusinessDTO dto) {
        businessService.addBusiness(dto);
        return Result.success();
    }
}
